package Hibernate.cn.edu.scau.cmi.lishuoxin.client;
import java.util.Set;

import Hibernate.cn.edu.scau.cmi.lishuoxin.dao.StudentDAO;
import Hibernate.cn.edu.scau.cmi.lishuoxin.dao.TeacherDAO;
import Hibernate.cn.edu.scau.cmi.lishuoxin.domain.Student;
import Hibernate.cn.edu.scau.cmi.lishuoxin.domain.Teacher;


public class StudentPane extends BasePane {

	private Student studentForAdd;
	private Student studentForUpdate;
	private Student studentForDelete;
	private Student studentForShow;
	private Student studentForSelected;
	private Teacher teacherForSelected;
	private Set studentSet;
	
	private StudentDAO studentDAO = new StudentDAO();
	private TeacherDAO teacherDAO = new TeacherDAO();
	private AlertBox alert = new AlertBox();
	
	public StudentPane() {
		super();
		init();
		setActions();
	}
	
	private void setActions() {
		addButton.setOnAction(e->addStudent());
		updateButton.setOnAction(e->updateStudent());
		deleteButton.setOnAction(e->deleteStudent());
		showButton.setOnAction(e->showStudent());
		selectButton.setOnAction(e->selectTeacher());
		updateSelectButton.setOnAction(e->updateSelectTeacher());
	}

	private void addStudent() {
		studentForAdd = new Student();
		try {
			studentForAdd.setId((long) Integer.parseInt(addForId.getText()));
			studentForAdd.setName(addForName.getText());
			studentDAO.save(studentForAdd);
			alert.display("添加成功");
		} catch (Exception e) {
			alert.display("添加失败");
		}
	}
	
	private void updateStudent() {
		try {
			studentForUpdate = studentDAO.findById((long) Integer.parseInt(updateForId.getText()));
			if(studentForUpdate==null) {
				alert.display("不存在该学生");
			}else {
				studentForUpdate.setName(updateForName.getText());
				studentDAO.update(studentForUpdate);
				alert.display("更新成功");
			}
		} catch (Exception e) {
			alert.display("更新失败");
		}
	}
	
	private void deleteStudent() {
		try {
			studentForDelete = studentDAO.findById((long) Integer.parseInt(deleteForId.getText()));
			if(studentForDelete==null) {
				alert.display("不存在该学生");
			}else {
				studentForDelete.setName(deleteForName.getText());
				studentDAO.delete(studentForDelete);
				alert.display("删除成功");
			}
		} catch (Exception e) {
			alert.display("删除失败");
		}
	}
	
	private void showStudent() {
		try {
			studentForShow = studentDAO.findById((long) Integer.parseInt(showForId.getText()));
			if(studentForShow==null) {
				alert.display("该学生不存在");
			}else {
				if(studentForShow.getTeacher()==null) {
					alert.display(studentForShow.toString());
				}else {
					alert.display(studentForShow.toString()+"指导老师 "+studentForShow.getTeacher().getName());
				}
			}
		} catch (Exception e) {
			alert.display("展示失败");
			e.printStackTrace();
		}
	}
	
	private void selectTeacher() {
		try {
			studentForSelected = studentDAO.findById((long) Integer.parseInt(selectForId.getText()));
			if(studentForSelected==null) {
				alert.display("该学生不存在");
			}else {
				teacherForSelected = teacherDAO.findById((long) Integer.parseInt(selectedForTutorId.getText()));
				if(teacherForSelected==null) {
					alert.display("该老师不存在");
				}else {
					studentForSelected.setTeacher(teacherForSelected);
					studentDAO.update(studentForSelected);
					alert.display("选择成功");
				}
			}
		} catch (Exception e) {
			alert.display("选择失败");
		}
	}
	
	private void updateSelectTeacher() {
		try {
			studentForSelected = studentDAO.findById((long) Integer.parseInt(updateSelectForId.getText()));
			if(studentForSelected==null) {
				alert.display("该学生不存在");
			}else {
				teacherForSelected = teacherDAO.findById((long) Integer.parseInt(updateSelectForTutorId.getText()));
				if(teacherForSelected==null) {
					alert.display("该老师不存在");
				}else {
					studentForSelected.setTeacher(teacherForSelected);
					studentDAO.update(studentForSelected);
					alert.display("更新选择成功");
				}
			}
		} catch (Exception e) {
			alert.display("更新选择失败");
		}
	}
}
